﻿namespace PowerSong.UserInterface
    {
    using System.Collections.Generic;
    using System.Windows;
    using PowerSong.SongDatabase;
    using System;
    using System.Linq;

    /// <summary>
    /// Interaction logic for EditCategoriesWindow.xaml
    /// </summary>
    public partial class EditCategoriesWindow : Window
        {

        public EditCategoriesWindow()
            {
            InitializeComponent();
            UpdateList();
            }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = true;
            }

        private void CreateCategoryButton_Click(object sender, RoutedEventArgs e)
            {
            var window = new AddCategoryWindow();
            if (window.ShowDialog() == true) UpdateList();
            }

        private void EditCategoryButton_Click(object sender, RoutedEventArgs e)
            {
            var category = (Category)CategoriesListBox.SelectedItem;
            if (category != null)
                {
                var window = new EditCategoryWindow(category);
                if (window.ShowDialog() == true) UpdateList();
                }
            }

        private void DeleteCategoryButton_Click(object sender, RoutedEventArgs e)
            {

            // Get the selected category
            var selectedCategory = (Category)CategoriesListBox.SelectedItem;
            if (selectedCategory != null)
                {

                // How many songs reference this category?
                var songCount = 0;
                foreach (var song in State.Database.Songs.GetAllSongs())
                    {
                    if (song.Categories.Contains(selectedCategory)) songCount += 1;
                    }

                // Confirm the delete
                var message = string.Format("Are you sure you wish to remove the category '{0}'?", selectedCategory.Name);
                if (songCount == 1) message += Environment.NewLine + Environment.NewLine + "Please note that there is 1 song that references the selected category. If you proceed, the category will obviously be removed from that song.";
                if (songCount > 1) message += Environment.NewLine + Environment.NewLine + "Please note that there are " + songCount + " songs that reference the selected category. If you proceed, the category will obviously be removed from those songs.";
                if (Utilities.Confirm("Delete Category", message))
                    {

                    // Get the songs assigned the category
                    var assignedSongs = State.Database.Categories.GetSongs(selectedCategory);

                    // Are there any songs which depend exclusively on the to-be-deleted category?
                    var exclusiveSongs = new List<Song>();
                    foreach (var song in assignedSongs)
                        {
                        if (song.Categories.Count == 1) exclusiveSongs.Add(song);
                        }
                    if (exclusiveSongs.Count > 0)
                        {
                        
                        // The user needs to fix a problem: some songs have been assigned only this category
                        var window = new DeleteCategoryWindow(selectedCategory);
                        if (window.ShowDialog() == true) UpdateList();

                        }
                    else
                        {

                        // Just need to remove the category from all the songs that have it (no special processing required!)
                        foreach (var song in assignedSongs)
                            {
                            song.Categories.Remove(selectedCategory);
                            State.Database.Songs.Write(song);
                            }

                        // Now delete the category itself
                        State.Database.Categories.Delete(selectedCategory);
                        UpdateList();

                        }                    

                    }

                }

            }

        private void CategoriesListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
            EditCategoryButton_Click(sender, e);
            }

        private void CategoriesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
            var categorySelected = CategoriesListBox.SelectedItems.Count > 0;
            EditCategoryButton.IsEnabled = categorySelected;
            DeleteCategoryButton.IsEnabled = categorySelected;
            ShowSongsButton.IsEnabled = categorySelected;
            }

        private void ShowSongsButton_Click(object sender, RoutedEventArgs e)
            {
            var selectedCategory = (Category)CategoriesListBox.SelectedItem;
            if (selectedCategory != null)
                {
                new ViewCategorySongsWindow(selectedCategory).ShowDialog();
                }
            }

        private void UpdateList()
            {
            CategoriesListBox.Items.Clear();
            var categories = State.Database.Categories.GetAllCategories().OrderBy(category => category.Name);
            foreach (var category in categories)
                {
                CategoriesListBox.Items.Add(category);
                }
            UpdateEnabledControls();
            }

        private void UpdateEnabledControls()
            {
            var categorySelected = CategoriesListBox.SelectedItem != null;
            this.EditCategoryButton.IsEnabled = categorySelected;
            this.DeleteCategoryButton.IsEnabled = categorySelected;
            this.ShowSongsButton.IsEnabled = categorySelected;
            }

        }

    }
